﻿using Edu.Abstracts;
using Edu.Entitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;

namespace Edu.Services
{
    public class WeChatUserAchievementDto
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Icon { get; set; }

        public string Desc { get; set; }
        public int Progress { get; set; }
        public DateTimeOffset? CreatedTime { get; set; }

        public bool Owned { get; set; }
    }

    public class WeChatAchievementAppService : WeChatAppServiceBase
    {

        private IdentityUserManager userManager;
        private IRepository<AppUserAchievement, Guid> repository;
        private IRepository<AppAchievement, Guid> achievementRepository;

        public WeChatAchievementAppService(IRepository<AppUserAchievement, Guid> repository,
            IRepository<AppAchievement, Guid> achievementRepository,
            IdentityUserManager userManager)
        {
            this.userManager = userManager;
            this.repository = repository;
            this.achievementRepository = achievementRepository;
        }

        public async Task<List<WeChatUserAchievementDto>> GetListAsync()
        {
            var achievements = await this.GetAchievementsByUid(this.CurrentUser.Id.Value);
            var owned = repository.Where(a => a.UserId == this.CurrentUser.Id.Value && a.CreatedTime != null).ToDictionary(a => a.AchievementId);

            var result = new List<WeChatUserAchievementDto>();

            foreach (var achievement in achievements)
            {
                var dto = new WeChatUserAchievementDto()
                {
                    Id = achievement.Id,
                    Name = achievement.Name,
                    Icon = achievement.DisableIcon,
                    Desc = achievement.Description,
                };

                if (owned.TryGetValue(achievement.Id, out var appUserAchievement))
                {
                    dto.Progress = appUserAchievement.Progress;
                    dto.CreatedTime = appUserAchievement.CreatedTime;
                    dto.Owned = true;
                    dto.Icon = achievement.ActiveIcon;
                }
                result.Add(dto);
            }
            result = result.OrderByDescending(v => v.Owned).ToList();
            return result;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        private async Task<List<AppAchievement>> GetAchievementsByUid(Guid userId)
        {
            var user = await userManager.GetByIdAsync(userId);
            var roles = await userManager.GetRolesAsync(user);

            var result = new List<AppAchievement>();
            if (roles.Contains(AchievementBelong.Student.ToString().ToLower()))
            {
                result = this.achievementRepository.Where(a => a.Belong == AchievementBelong.Student).ToList();
            }
            else if (roles.Contains(AchievementBelong.Teacher.ToString().ToLower()))
            {
                result = this.achievementRepository.Where(a => a.Belong == AchievementBelong.Teacher).ToList();
            }

            return result;
        }
    }
}
